
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.common.Constants;
import com.yimixian.app.model.Metadata;
import com.yimixian.app.model.ValidatedOrder;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.List;

public class PricesView extends LinearLayout {

    @InjectView(R.id.price_name_text)
    TextView mPriceNameText;
    @InjectView(R.id.price_value_text)
    TextView mPriceValueText;
    @InjectView(R.id.price_to_pay_text)
    TextView mPriceToPayText;

    public PricesView(Context context) {
        super(context);
        initPricesView();
    }

    public PricesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPricesView();
    }

    public PricesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPricesView();
    }

    private void initPricesView() {
        LayoutInflater.from(getContext()).inflate(R.layout.prices_view, this);
        ButterKnife.inject(this);
        setOrientation(VERTICAL);
        setBackgroundColor(Color.WHITE);
    }

    public void bindView(List<ValidatedOrder.PriceComponents> prices, String rawValue) {
        StringBuilder priceNameBuilder = new StringBuilder();
        StringBuilder priceValueBuilder = new StringBuilder();
        for (int i = 0; i < prices.size(); ++i) {
            if (i > 0) {
                priceNameBuilder.append(Constants.SEPARATOR);
                priceValueBuilder.append(Constants.SEPARATOR);
            }
            priceNameBuilder.append(prices.get(i).getName());

            priceValueBuilder.append(prices.get(i).getValue());

        }
        mPriceNameText.setText(priceNameBuilder);
        mPriceValueText.setText(priceValueBuilder);
        mPriceToPayText.setText(rawValue);
    }

    public void bind(List<Metadata> prices, String priceToPay) {
        StringBuilder priceNameBuilder = new StringBuilder();
        StringBuilder priceValueBuilder = new StringBuilder();
        for (int i = 0; i < prices.size(); ++i) {
            if (i > 0) {
                priceNameBuilder.append(Constants.SEPARATOR);
                priceValueBuilder.append(Constants.SEPARATOR);
            }
            priceNameBuilder.append(prices.get(i).name);

      //      Double value = Double.valueOf(prices.get(i).value);
            priceValueBuilder.append(prices.get(i).value);
//            if (value > 0) {
//                priceValueBuilder.append(Constants.YUAN + " " + String.format("%.2f", value));
//            } else if (value == 0.0f) {
//                if (prices.get(i).value.startsWith("-")) {
//                    priceValueBuilder.append("- " + Constants.YUAN + " "
//                            + String.format("%.2f", value * -1));
//                } else {
//                    priceValueBuilder.append(Constants.YUAN + " " + String.format("%.2f", value));
//                }
//            } else {
//                priceValueBuilder.append("- " + Constants.YUAN + " "
//                        + String.format("%.2f", value * -1));
//            }
        }
        mPriceNameText.setText(priceNameBuilder);
        mPriceValueText.setText(priceValueBuilder);
        mPriceToPayText.setText(priceToPay);
//        mPriceToPayText.setText(Constants.YUAN + " "
//                + String.format("%.2f", Double.valueOf(priceToPay)));
    }
}
